package net.gyapp.markim.module.mine.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.Helper;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.basic.event.AccountChanged;
import net.gyapp.markim.module.basic.event.AccountIml;
import net.gyapp.markim.utils.DeviceUtils;
import net.gyapp.markim.utils.RequestParams;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/7/6
 * Version: 1.0
 * Desc:    账号注册
 */


public class RegisterActivity extends WrapperSwipeActivity<CommonPresenter> {

    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_cipher)
    EditText etCipher;
    @BindView(R.id.et_recommend)
    EditText etRecommend;

    public static Intent getIntent(Context context) {
        return new Intent(context, RegisterActivity.class);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("账号注册");

    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if (url.contains(ApiConfig.API_RIGISTER)) {
            processRegisterSuccess();
        }
    }


    @OnClick({R.id.btn_register})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.btn_register:
                String account = etAccount.getText().toString();
                if (TextUtils.isEmpty(account)) {
                    showToast("请设置账号");
                    return;
                }
                if (account.length() < 6) {
                    showToast("请输入6-20数字或字母");
                    return;
                }

                String cipher = etCipher.getText().toString();
                if (TextUtils.isEmpty(cipher)) {
                    showToast("请设置密码");
                    return;
                }

                if (cipher.length() < 6) {
                    showToast("请输入最少6个字符的密码");
                    return;
                }

                String recommend = etRecommend.getText().toString();

                register(account, cipher, recommend);
                break;
        }
    }

    private void register(String account, String cipher, String recommend) {
        presenter.postData(ApiConfig.API_RIGISTER, new RequestParams().put("userName", account).put("RefereeId", recommend).put("password", cipher).put("deviceToken", DeviceUtils.getDeviceId()).get(), BaseVo.class);
    }

    private void processRegisterSuccess() {
        Helper.setAccount(etAccount.getText().toString());
        EventBus.getDefault().post(new AccountChanged(AccountIml.ACCOUNT_REQISTER));
        startActivity(LoginActivity.getIntent(mActivity));
        finish();
    }
}
